<?php
/**
* 添加/修改帖子页面
* $Id: new_edit_article.php 238 2011-01-13 08:53:47Z bwanglei@gmail.com $
*/

require_once './globals.php';

$blog_title = '添加&修改帖子 - '.$blogname;
$tag_cache = $CACHE->readCache('tags');

if ($action == 'edit' && $_SERVER['REQUEST_METHOD'] == 'GET') {
	require_once AW_ROOT.'/model/class.blog.php';
	require_once AW_ROOT.'/model/class.tag.php';

	$aw_blog = new awBlog();
	$aw_tag = new awTag();

	if (!isset($_GET['aid'])) die('不存在的帖子');
	$article_id = $_GET['aid'];
	$article = $aw_blog->getOneLogForAdmin($article_id);
	$tags = $aw_tag->getTag($article_id);
}

if ($action == 'upload-file' && $_SERVER['REQUEST_METHOD']=='POST') {
	$ext_name= extend($_FILES['upload_file']['name']);	// 取得文件的扩展名
	if ($_FILES['upload_file']['error'] > 0 || $_FILES['fujian_file']['size'] > $file_size) {
			echo '上传错误，或者文件太大';
	} else if (!in_array($ext_name,array('gif','png','jpg','jpeg'))) {
			echo '文件格式必须为 gif， png 或者 jgp,jpeg。';
	} else {
		//保存附件到upload文件夹
		$file_name = time().'_'.$_FILES['upload_file']['name'];
		$file_name_url = $blogUrl.'/upload/'.$file_name;
		$file_path = AW_ROOT.'/upload/'.$file_name;
		move_uploaded_file($_FILES['upload_file']['tmp_name'],$file_path);

		//把文件url存入数据库
		$query_insert_file = "insert into `".DB_PREFIX."files` (`url`,`name`) values ('".$file_name_url."','".$file_name."')";
		$DB->query($query_insert_file);
		$insert_id = $DB->insert_id();
	}
include_once TEMPLATE_PATH.'/upload_file_iframe.php'; exit;
}

if ($action == 'save-article' && $_SERVER['REQUEST_METHOD'] == 'POST') {
	require_once AW_ROOT.'/model/class.blog.php';
	require_once AW_ROOT.'/model/class.tag.php';
	$aw_blog = new awBlog();
	$aw_tag = new awTag();

	$title_article = isset($_POST['title_article']) ? $_POST['title_article'] : '';
	$content_article = isset($_POST['content_article']) ? $_POST['content_article'] : '';
	$tags_article = isset($_POST['tags_article']) ? $_POST['tags_article'] : '';
	$type_article = in_array($_POST['type_article'],array('draft','article','page')) ? $_POST['type_article'] : '';
	$article_array = array(
		'title'=>$title_article,
		'content'=>$content_article,
		'time_date'=>time(),
		'type'=>$type_article
	);
	if (!isset($_GET['aid'])) {
		$article_id = $aw_blog->addlog($article_array);
		$aw_tag->addTag($tags_article,$article_id);
	} else {
		$article_id = $_GET['aid'];
		$aw_blog->updateLog($article_array, $article_id);
		$aw_tag->updateTag($tags_article, $article_id);
	}
	$CACHE->updateCache(array('sta','tags','record','pages'));	//更新缓存
	header('Location: '.$blogUrl.'/admin/new_edit_article.php?action=edit&aid='.$article_id); exit;
}
include_once TEMPLATE_PATH.'/header.php';
include_once TEMPLATE_PATH.'/new_edit_article.php';
include_once TEMPLATE_PATH.'/sidebar.php';
include_once TEMPLATE_PATH.'/footer.php';
